package com.demo1.controller;

import cn.hutool.core.util.IdUtil;
import com.demo1.common.ResponseResult;
import com.demo1.dao.TagsMapper;
import com.demo1.util.RedisStreamUtil;
import com.demo1.util.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;




@RestController
@Api(tags = "测试")
@RequiredArgsConstructor
@Slf4j
public class Controller {


   private final TagsMapper tagsMapper;


    private final RedisUtils redisUtils;
    private final RedisStreamUtil redisStreamUtil;
    public static final String RED_PACKAGE_KEY = "redpackage:";
    public static final String RED_PACKAGE_CONSUME_KEY = "redpackage:consume:";


    @GetMapping("/sendRedPackage")
    @ApiOperation("发送红包")
    public ResponseResult sendRedPackage(int totalMoney,int redPackageNumber){
        Integer[] splitRedPackages=splitRedPackage(totalMoney,redPackageNumber);
        String redisKey=RED_PACKAGE_KEY+ IdUtil.getSnowflakeNextId();
        Arrays.stream(splitRedPackages).forEach(e->{
            redisUtils.lLeftPush(redisKey,e.toString());
        });
        redisUtils.expire(redisKey,1, TimeUnit.DAYS);
        return ResponseResult.success(redisKey);
    }

    @GetMapping("/robRedPackage")
    @ApiOperation("抢红包")
    public ResponseResult robRedPackage(String redPackageKey,String userId){

        log.info("开始时间");
        String streamKey = "redis:mq:streams:key2";

        Object redPackage=redisUtils.hGet(RED_PACKAGE_CONSUME_KEY+redPackageKey,userId);
        if(redPackage==null){
            Object partRedPackage=redisUtils.lLeftPop(RED_PACKAGE_KEY+redPackageKey);
            if(partRedPackage!=null){
                redisUtils.hPut(RED_PACKAGE_CONSUME_KEY+redPackageKey,userId,partRedPackage.toString());
                System.out.println("用户"+userId+"抢到多少钱红包"+partRedPackage);

                Map<String,Object> message=new HashMap<>();
                String type="redPackage";
                message.put("type",type);
                message.put("redPackage",partRedPackage);
                message.put("userId",userId);
                redisStreamUtil.addMap(streamKey,message);
                type="notice";
                message.put("type",type);
                message.put("notice","通知");
                redisStreamUtil.addMap(streamKey,message);
                type="log";
                message.put("type",type);
                message.put("log","存储记录");
                redisStreamUtil.addMap(streamKey,message);

                return ResponseResult.success(partRedPackage.toString());
            }
            return ResponseResult.success("红包被抢完了");
        }
        return ResponseResult.error("你已经抢过红包了");
    }

    @GetMapping("/testStream")
    public String testStream() {
        HashMap<String, Object> message = new HashMap<>(2);
        message.put("body", "消息主题" );
        message.put("sendTime", "消息发送时间");
        String streamKey = "redis:mq:streams:key2";
        redisStreamUtil.addMap(streamKey, message);
        return "testStream";
    }


    private Integer[] splitRedPackage(int totalMoney,int redPackageNumber){
        int useMoney=0;
        Integer[] redPackageNumbers=new Integer[redPackageNumber];
        Random random=new Random();
        for(int i=0;i<redPackageNumber;i++){
            if(i==redPackageNumber-1){
                redPackageNumbers[i]=totalMoney-useMoney;
            } else {
                int avgMoney=((totalMoney-useMoney)/(redPackageNumber-i))*2;
                redPackageNumbers[i]=1+ random.nextInt(avgMoney-1);
            }
            useMoney=useMoney+redPackageNumbers[i];
        }
        return  redPackageNumbers;
    }



}
